package 泛型;

import java.util.ArrayList;

import java.util.Collections;

//定义实现Comparable接口的类
class Student  implements Comparable<Student>
{
	String name;
	int age;
	
	Student(String name, int age)
	{
		this.name=name;
		this.age=age;
	}
	void printInfo()
	{
		System.out.println(name +"\t"+age);
		
	}
	//重写comparable接口中的comoarable方法
	//比较年龄
	/*public int compareTo(Student s)
	{
		return this.age-s.age;
		
	}*/
	public int compareTo(Student o)
	{
		return this.name.compareTo(o.name);
		
	}
}

public class ComparableDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s1=new Student("Jacky",21);
		Student s2=new Student("Frank",20);
		Student s3=new Student("Annie",19);
		ArrayList<Student>a1=new ArrayList<Student >();
		a1.add(s1);
		a1.add(s2);
		a1.add(s3);
		//排序
		Collections.sort(a1);//自动调用上面的CompareTo()方法
		//打印
		a1.forEach(e->e.printInfo());
		
		

	}

}
